
#include "HX711.h"
u32 pi_zhong = 0;
u32 calibration_val = 232;
u8 flag_error = 0;

void hx711Config(char *xb){//
	char sck_data_gpio[6];
	snprintf(sck_data_gpio,6,"%s|5",myStrsep(xb, "|",&xb));//5为u8 gpio_mode_cb[8] = {索引
	gpioInit(sck_data_gpio);
	snprintf(sck_data_gpio,6,"%s|3",xb);//3为u8 gpio_mode_cb[8] = {索引
	gpioInit(sck_data_gpio);
	/*
	char *gpio_x_sck = myStrsep(xb, "|",&xb);
	u8 pin_x_sck_i = strtol(gpio_x + 1, &endptr_str, 10);
	u8 gpio_mode_i = strtol(myStrsep(str, "|",&str), &endptr_str, 10);
	gpioInit(char *str);
	gpioInit(char *str);
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);//使能端口时钟

	//HX711_SCK
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//端口配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHz
	GPIO_Init(GPIOG, &GPIO_InitStructure);//根据设定参数初始化GPIOB

        //HX711_DOUT
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输入
	GPIO_Init(GPIOG, &GPIO_InitStructure);
	*/
	
	hx711QuPi();//去皮
	delay_ms(1000);delay_ms(1000);
	hx711QuPi();//重新去皮
	printf("hx711Config complete\r\n");
	
}
//****************************************************
//读取HX711,增益128
//****************************************************
u32 hx711Read(void){
	unsigned long count; 
	unsigned char i; 
	GPIO_ResetBits(GPIOG,GPIO_Pin_3);//HX711_SCK=0;//
	count=0; 
	delay_us(1);
	while(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_2));//HX711_DOUT
	for(i=0;i<24;i++){ 
		GPIO_SetBits(GPIOG,GPIO_Pin_3);//HX711_SCK=1;//
		count=count<<1; 
		delay_us(1);
    GPIO_ResetBits(GPIOG,GPIO_Pin_3);//HX711_SCK=0;//
    if(GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_2))count++;//HX711_DOUT
		delay_us(1);
  } 
  GPIO_SetBits(GPIOG,GPIO_Pin_3);//HX711_SCK=1;//
	count=count^0x800000;//第25个脉冲下降沿来时，转换数据
  delay_us(1);
  GPIO_ResetBits(GPIOG,GPIO_Pin_3);//HX711_SCK=0;
  return(count);
}

//****************************************************
//去皮
//****************************************************
void hx711QuPi(void){
	pi_zhong = hx711Read();
}

void hx711JiaoZhun(char *xb){
	u16 i = 500;
	char *endptr_str;
	unsigned int Reference_value = strtol(myStrsep(xb, "|",&xb), &endptr_str, 10);
	unsigned int error_value = strtol(xb, &endptr_str, 10);
	int temp = 0;
	char rst[26] = "hx711JiaoZhun fail";
	u32 mao_zhong = 0,jin_zhong = 0;
	while(i--){
		mao_zhong = hx711Read();
		if(mao_zhong > pi_zhong){
			jin_zhong = (((float)mao_zhong - pi_zhong)/i);
			temp = jin_zhong - Reference_value;
			if(fabs(temp) < error_value){
				snprintf(rst,26,"%s","hx711JiaoZhun success");
				calibration_val = i;
				printf("mao_zhong %u pi_zhong %u jin_zhong %u error_value %d i:%d\r\n",mao_zhong,pi_zhong,jin_zhong,temp,i);
				break;
			}
		}
		printf("mao_zhong %u pi_zhong %u jin_zhong %u error_value %d i:%d\r\n",mao_zhong,pi_zhong,jin_zhong,temp,i);
	}
	printf("%s",rst);
}

//****************************************************
//称重
//****************************************************
u32 hx711GetWeight(void){
	u32 mao_zhong = hx711Read();
	u32 j_zhong = 0;
	if(mao_zhong > pi_zhong){
		j_zhong = (u32)(((float)mao_zhong - pi_zhong)/calibration_val);
	}
	printf("%u\r\n",j_zhong);
	return j_zhong;
}
